package mi.payment.utils.serial;

import android.content.Context;
import android.hardware.usb.UsbManager;
import android.widget.Toast;

import mi.payment.MyApplication;
import tw.com.prolific.driver.pl2303.PL2303Driver;

/**
 * Created by longx on 2017/7/11.
 */

public class SerialPortUtils {


    private static final String USB_PERMISSION = "com.prolific.pl2303hxdsimpletest.USB_PERMISSION";
    private static SerialPortUtils instance;
    private PL2303Driver mSerial=null;
    private PL2303Driver.BaudRate mBaudrate = PL2303Driver.BaudRate.B9600;
    private PL2303Driver.DataBits mDataBits = PL2303Driver.DataBits.D8;
    private PL2303Driver.Parity mParity = PL2303Driver.Parity.NONE;
    private PL2303Driver.StopBits mStopBits = PL2303Driver.StopBits.S1;
    private PL2303Driver.FlowControl mFlowControl = PL2303Driver.FlowControl.OFF;

    public static SerialPortUtils getInstance(){
        if(instance==null){
            instance = new SerialPortUtils();
        }
        return instance;
    }

    /**
     * 读取data
     * @return
     */
    public byte[] readDataFromSerialPort(){
        if(null==mSerial){
            return null;
        }
        if(!mSerial.isConnected()){
            return null;
        }
        int len;
        byte[] data = new byte[1];
        if(!mSerial.isConnected()){
            return null;
        }
        len = mSerial.read(data);
        if(len<0) {
            return null;
        }
        if (len > 0) {
            return data;
        }
        return null;
    }

    /**
     * 发送数据
     * @param data
     * @return
     */
    public boolean sendDataToSerialPort(byte[] data){
        if (mSerial == null)return false;
        if (!mSerial.isConnected())return false;
        if(data==null) return false;
        int res = mSerial.write(data,data.length);
        if(res<0){
            showToast("发送失败！code："+res);
            return false;
        }
        return true;
    }

    /**
     * 初始化串口
     */
    public void initSerialPort(){
        this.mSerial = new PL2303Driver((UsbManager) MyApplication.getInstance().getSystemService(Context.USB_SERVICE), MyApplication.getInstance(), USB_PERMISSION);
    }

    /**
     * 初始化串口的参数
     * @return
     */
    public boolean initSerialParameter(){
        if(mSerial==null){
            showToast("串口初始化失败！");
            return false;
        }
        if(!mSerial.InitByPortSetting(mBaudrate, mDataBits, mStopBits, mParity, mFlowControl)){
            if(!mSerial.PL2303Device_IsHasPermission()) {
                showToast("串口初始化失败！无权限");
                return false;
            }

            if(mSerial.PL2303Device_IsHasPermission() && (!mSerial.PL2303Device_IsSupportChip())) {
                showToast("串口初始化失败！芯片不支持");
                return false;
            }
        } else {
            mSerial.PL2303Device_SetCommTimeouts(2000);
            showToast("串口已连接");
            return true;

        }
        return true;
    }

    public boolean applySerial(){
        if(mSerial==null){
            showToast("打开串口失败！");
            return false;
        }
        if(!mSerial.isConnected()) {
            boolean isEnumerate = mSerial.enumerate();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(!isEnumerate){
                showToast("打开串口失败！");
                return false;
            }
        }
        return true;
    }

    private void showToast(String msg){
        Toast.makeText(MyApplication.getInstance(),msg, Toast.LENGTH_SHORT).show();
    }

}

